﻿using Abp.Application.Services;
using Abp.Collections.Extensions;
using Abp.Domain.Repositories;
using Microsoft.AspNetCore.Http;
using SportsPlatform.Base;
using SportsPlatform.Consts;
using SportsPlatform.Enums;
using SportsPlatform.Helpers;
using SportsPlatform.Model.PermissionManagement;
using SportsPlatform.Model.SportBusiness;
using SportsPlatform.Query.SportBusiness.Feedback;
using SportsPlatform.SportBusiness.ISportBusiness;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SportsPlatform.SportBusiness.SportBusiness
{
    /// <summary>
    /// 意见反馈接口实现服务
    /// </summary>
    [RemoteService(false)]  //隐藏自动生成的WebApi
    public class FeedbackService : SportsPlatformAppServiceBase, IFeedbackService
    {
        private readonly IRepository<Feedback, Guid> _repository;

        private readonly IRepository<Employee, Guid> _userRepository;
        private readonly IRepository<CompanyInfo, Guid> _compantInfoRepository;
        public FeedbackService(IRepository<Feedback, Guid> repository, IRepository<Employee, Guid> userrepository,
            IRepository<CompanyInfo, Guid> compantInfoRepository, IHttpContextAccessor context) : base(context)
        {
            _repository = repository;
            _userRepository = userrepository;
            _compantInfoRepository = compantInfoRepository;
        }



        /// <summary>
        /// 获取分页后的意见反馈数据
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public async Task<PagePagingDto<FeedbackDto>> GetFeedback(GetFeedbackQuery query)
        {
            PagePagingDto<FeedbackDto> result = new PagePagingDto<FeedbackDto>();
            try
            {
                List<FeedbackDto> feedbacks = new List<FeedbackDto>();
                var asyncres = await _repository.GetAllListAsync();

                if (asyncres.Count > 0)
                {
                    foreach (var item in asyncres)
                    {
                        var user = await _userRepository.GetAsync(item.UserId);

                        if (user.IsNullAndEmpty())
                        {
                            result.Code = HttpResultCode.SqlError;
                            result.Message = "未查询数据";
                            result.Data = null;
                            return result;
                        }
                        feedbacks.Add(new FeedbackDto
                        {
                            CommitTime = item.CommitTime.ToString("yyyy-MM-dd"),
                            Content = item.Content,
                            Source = Enum.GetName(typeof(SourceEnum), item.Source),
                            UserId = item.UserId,
                            UserName = user.RealName,
                            UserTel = user.Phone.Substring(0, 3) + "****" + user.Phone.Substring(user.Phone.Length - 4, 4),
                        });
                    }
                    var res = feedbacks.WhereIf(!query.NameOrShopName.IsNullAndEmpty(), f => f.UserName.Contains(query.NameOrShopName) || f.UserTel.Contains(query.NameOrShopName))
                        .WhereIf(!query.StartTime.IsNullAndEmpty() && !query.EndTime.IsNullAndEmpty(), f => Convert.ToDateTime(f.CommitTime) >= Convert.ToDateTime(query.StartTime)
                        && Convert.ToDateTime(f.CommitTime) <= Convert.ToDateTime(query.EndTime))
                        .OrderByDescending(f => Convert.ToDateTime(f.CommitTime)).ToList();

                    result.TotalCount = feedbacks.Count;
                    result.Code = HttpResultCode.Success;
                    result.IsSuccess = true;
                    result.Message = "查询成功";
                    result.Data = res.Skip(query.PageSize * (query.PageIndex - 1)).Take(query.PageSize);
                }
                else
                {
                    result.Code = HttpResultCode.SqlError;
                    result.Message = "未查询数据";
                    result.Data = null;
                }

            }
            catch (Exception e)
            {
                result.Code = HttpResultCode.Error;
                result.IsSuccess = false;
                result.Message = e.Message.ToString();
            }
            return result;
        }
    }
}
